package com.dearbinge.data.service.impl.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.dearbinge.data.api.admin.AdminSecurityAuthorityService;
import com.dearbinge.data.dao.admin.AdminSecurityAuthorityMapper;
import com.dearbinge.data.pojo.admin.AdminSecurityAuthorityDO;
import com.dearbinge.data.pojo.admin.vo.AdminSecurityAuthorityRequestVO;
import com.dearbinge.data.pojo.admin.vo.AdminSecurityAuthorityResponseVO;
import com.dearbinge.utils.ListToTree;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 后台权限接口实现层
 *
 * @author wes
 * @date 2019/4/11
 */
@Service("adminSecurityAuthorityService")
public class AdminSecurityAuthorityServiceImpl implements AdminSecurityAuthorityService {

	@Autowired
	private AdminSecurityAuthorityMapper adminSecurityAuthorityMapper;


	/**
	 * 删除
	 *
	 * @param authorityId 权限表id
	 * @return 影响行数
	 */
	@Override
	public Integer delete(Integer authorityId) {
		return adminSecurityAuthorityMapper.delete(authorityId);
	}

	/**
	 * 根据id查询
	 *
	 * @param authorityId 权限表id
	 * @return 返回VO
	 */
	@Override
	public AdminSecurityAuthorityResponseVO getById(Integer authorityId) {
		AdminSecurityAuthorityDO adminSecurityAuthorityDO = adminSecurityAuthorityMapper.getById(authorityId);
		if (adminSecurityAuthorityDO == null) {
			return null;
		}
		AdminSecurityAuthorityResponseVO adminSecurityAuthorityResponseVO = new AdminSecurityAuthorityResponseVO();
		BeanUtils.copyProperties(adminSecurityAuthorityDO,adminSecurityAuthorityResponseVO);
		return adminSecurityAuthorityResponseVO;
	}

	/**
	 * 更新
	 *
	 * @param adminSecurityAuthorityRequestVO 请求VO
	 * @return 影响行数
	 */
	@Override
	public Integer update(AdminSecurityAuthorityRequestVO adminSecurityAuthorityRequestVO) {
		AdminSecurityAuthorityDO adminSecurityAuthorityDO = new AdminSecurityAuthorityDO();
		BeanUtils.copyProperties(adminSecurityAuthorityRequestVO,adminSecurityAuthorityDO);
		return adminSecurityAuthorityMapper.update(adminSecurityAuthorityDO);
	}

	/**
	 * 插入
	 *
	 * @param adminSecurityAuthorityRequestVO 请求VO
	 * @return 影响行数
	 */
	@Override
	public Integer insert(AdminSecurityAuthorityRequestVO adminSecurityAuthorityRequestVO) {
		AdminSecurityAuthorityDO adminSecurityAuthorityDO = new AdminSecurityAuthorityDO();
		BeanUtils.copyProperties(adminSecurityAuthorityRequestVO,adminSecurityAuthorityDO);
		return adminSecurityAuthorityMapper.insert(adminSecurityAuthorityDO);
	}

	/**
	 * 查询全部权限
	 * @param roleId 角色id
	 * @return 全部权限
	 */
	@Override
	public JSONArray listAll(Integer roleId) {
		List<AdminSecurityAuthorityResponseVO> data = adminSecurityAuthorityMapper.listAll(roleId);
		return ListToTree.listToTree(JSONArray.parseArray(JSON.toJSONString(data)),"authorityId","parentId","children");
	}
}